package utl.mofen.jsonStreamHandler;

import org.apache.log4j.Logger;

import com.google.gson.JsonObject;

import utl.mofen.objects.JSonStream;
import utl.mofen.utils.GUID;

public class JSonStreamHandler implements IJSonStreamHandler {
	protected final Logger logger = Logger.getLogger(this.getClass());
	protected String key = utl.mofen.utils.GUID.createGUID();
	protected JSonStream jsonStream = null;


	public void setJSonStream(JSonStream obj) {
		jsonStream = obj;
	}

	public void doAction() throws Exception {
				
	}

	public HandlerSession getSession() {
		if (this.jsonStream==null) return null;
		if (this.jsonStream.getJSonObject()==null) return null;
		
		JsonObject json = this.jsonStream.getJSonObject();
		
		if (!json.has("config"))
		{
			json.add("config", new JsonObject());
		}
		
		if (!json.get("config").getAsJsonObject().has("sessionid"))
		{
			json.get("config").getAsJsonObject().addProperty("sessionid", GUID.createGUID());
		}
		
		return HandlerSession.getHandlerSession(json.get("config").getAsJsonObject().get("sessionid").getAsString());
	}
}
